/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package server;

import database.Item;
import database.ItemFacadeLocal;
import database.ItemType;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Ewoud
 */
@WebServlet(name = "NewItemServlet", urlPatterns = {"/NewItemServlet"})
public class NewItemServlet extends HttpServlet {
    @EJB
    private ItemFacadeLocal itemFacade;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            
            //get parameters
            String name = request.getParameter("name");
            float price = Float.parseFloat(request.getParameter("price"));
            String description = request.getParameter("description");
            String typeString = request.getParameter("type");
            ItemType type;
            switch(typeString){
                case "BREAD":
                    type = ItemType.BREAD;
                    break;
                case "PASTRY":
                    type = ItemType.PASTRY;
                    break;
                case "PIE":
                    type = ItemType.PIE;
                    break;
                case "CAKE":
                    type = ItemType.CAKE;
                    break;
                case "PREPARED_SANDWICHE":
                    type = ItemType.PREPARED_SANDWICHE;
                    break;
                default:
                    //this case should not happen but anyhow
                    type = ItemType.BREAD; 
            }
            
            //show page
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet newItemServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Adding new item to database...</h1>");
            out.println("<hr />");
            out.println(name+"<br />");
            out.println(price+"<br />");
            out.println(description+"<br />");
            out.println(typeString);
            out.println("<hr />");

            
            //save in DB
            Item newItem = new Item();
            newItem.setName(name);
            newItem.setPrice(price);
            newItem.setDescription(description);
            newItem.setType(type);
            itemFacade.create(newItem);
            
            out.println("<h1>Succes!</h1>");
            
            out.println("<hr />");
            List<Item> items = itemFacade.findAll();
            for(Item currentItem : items){
                out.println( currentItem.getName()+" "+currentItem.getPrice()
                        +" "+currentItem.getDescription()+" "
                        +currentItem.getType()+"<br />" );
            }
            out.println("</body>");
            out.println("</html>");
            

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
